<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      v-model="form"
      :data="data"
      :page.sync="page"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button type="primary" v-if="permission.rollingMaterial_export" size="small" icon="el-icon-download" @click="handleExport">导出</el-button>
      </template>
      <template slot="menuRight">
        <user-table-config :option="option" @confirm="(val) => handleSetUserConfig(val, pageCode)"></user-table-config>
      </template>
    </avue-crud>
    <export-comp ref="exportComp" page-api-url="mes-warehouse/material/rolling/page" :option="option" :page="page" :query="exportParams" file-name="滚动发料记录"> </export-comp>
  </basic-container>
</template>
<script>
import { mapGetters } from 'vuex';
import exportComp from '@/components/export-comp';
import userTableConfig from '@/components/user-table-config';
import userConfig from '@/mixins/userConfig';
import { getList } from '@/api/warehouse/rollingMaterial';
export default {
  name: 'rollingMaterial', // 会渲染成传入的componentName
  components: {
    userTableConfig,
    exportComp
  },
  mixins: [userConfig()],
  data() {
    return {
      form: {},
      query: {
        startTime: this.getDefaultTime()[0],
        endTime: this.getDefaultTime()[1]
      },
      loading: true,
      page: {
        pageSize: 20,
        currentPage: 1,
        total: 0
      },
      exportParams: {},
      data: [],
      option: {
        calcHeight: 30,
        height: 'calc(100vh - 485px)',
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        searchSpan: 5,
        border: true,
        index: true,
        viewBtn: false,
        editBtn: false,
        delBtn: false,
        addBtn: false,
        menu: false,
        indexLabel: '序号',
        columnBtn: false,
        searchLabelWidth: 100,
        column: [
          {
            label: '物料名称',
            prop: 'partName'
          },
          {
            label: '物料编码',
            prop: 'partNo',
            search: true,
            searchOrder: 1
          },
          {
            label: '供应商批次',
            prop: 'lotNo',
            search: true,
            searchOrder: 2
          },
          // {
          //   label: '状态',
          //   prop: 'statusName'
          // },
          {
            label: '状态',
            prop: 'status',
            type: 'select',
            search: true,
            dicData: [
              {
                label: '待滚动',
                value: 1
              },
              {
                label: '已滚动',
                value: 2
              }
            ]
          },
          {
            label: '滚动数量',
            prop: 'quantity'
          },
          {
            label: '滚动时间',
            prop: 'lastRollingTime'
          },
          {
            label: '操作时间',
            prop: 'time',
            searchType: 'datetimerange',
            format: 'yyyy-MM-dd HH:mm:ss',
            valueFormat: 'yyyy-MM-dd HH:mm:ss',
            startPlaceholder: '开始日期',
            endPlaceholder: '结束日期',
            hide: true,
            searchSpan: 8,
            search: true,
            searchOrder: 4,
            viewDisplay: false,
            searchValue: this.getDefaultTime()
          },
          {
            label: 'DC',
            prop: 'dateCode'
          },
          {
            label: '备料单号',
            prop: 'sheetNo'
          },
          {
            label: '领料单号',
            prop: 'poSheetNo',
            search: true,
            searchOrder: 5
          },
          {
            label: '客户名称',
            prop: 'partnerName'
          },
          {
            label: '客户编码',
            prop: 'partnerCode'
          },
          {
            label: '工单号',
            prop: 'workOrderNo',
            search: true,
            searchOrder: 6
          },
          {
            label: '产品编码',
            prop: 'workOrderPartNo',
            search: true,
            searchOrder: 3
          },
          {
            label: '产品名称',
            prop: 'workOrderPartName'
          },
          {
            label: '产品型号',
            prop: 'workOrderPartModel'
          }
        ]
      }
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.$structuralSplit();
    });
  },
  computed: {
    ...mapGetters(['permission'])
  },
  methods: {
    getDefaultTime() {
      const startTime = this.$moment(new Date()).startOf('month').format('yyyy-MM-DD HH:mm:ss');
      const endTime = this.$moment(new Date()).format('yyyy-MM-DD HH:mm:ss');
      return [startTime, endTime];
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      if (params.time && params.time.length > 0) {
        params.startTime = params.time[0];
        params.endTime = params.time[1];
        delete params.time;
      }
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    async onLoad(page, params = {}) {
      this.loading = true;
      try {
        const res = await getList(page.currentPage, page.pageSize, Object.assign(params, this.query));
        const data = res.data.data;
        this.data = data.records;
        this.page.total = data.total;
        this.page.currentPage = data.current;
      } catch (e) {
        console.warn(e);
      } finally {
        this.loading = false;
      }
    },
    handleExport() {
      const exportParams = this._cloneDeep(this.query);
      if (exportParams.time && Array.isArray(exportParams.time)) {
        exportParams.startTime = this.$moment(exportParams.time[0]).format('YYYY-MM-DD HH:mm:ss');
        exportParams.endTime = this.$moment(exportParams.time[1]).format('YYYY-MM-DD HH:mm:ss');
        delete exportParams.time;
      }
      this.$refs.exportComp.visible = true;
    }
  }
};
</script>
